angular.module('app', ['ionic'])

    .config(function($stateProvider, $urlRouterProvider){
        'use strict';
        $stateProvider
            .state('app', {
                url: '/app',
                templateUrl: 'views/app.html',
                controller: 'AppCtrl'
            });
        $urlRouterProvider.otherwise('/app');
    })

    .run(function($ionicPlatform) {
        'use strict';
        $ionicPlatform.ready(function() {
            if(window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if(window.StatusBar) {
                StatusBar.styleDefault();
            }
        });
    })

    .factory('TwittSrv', function($q, $timeout){
        'use strict';
        var twitts = [
            {user: {id: '凤舞九天', name: '1、吴明', avatar: 'https://pbs.twimg.com/profile_images/459365829348302849/lJ1X8rU9_bigger.png'}, content: '江湖绰号“小老头”。《凤舞九天》第一高手，无名岛岛主。实力相当于3个绝顶高手。', url: 'https://twitter.com/Ionicframework/status/569962126556708864'},
            {user: {id: '画眉鸟', name: '2、阴姬', avatar: 'https://pbs.twimg.com/profile_images/554941644579954688/xxp8o3Cq_bigger.png'}, content: '人称“水母”。《画眉鸟》第一高手，神水宫主人。实力相当于2.5个绝顶高手。', url: 'https://twitter.com/IonicBE/status/569787372650385408'},
            {user: {id: '绝代双娇', name: '3、燕南天', avatar: 'https://pbs.twimg.com/profile_images/539837685481762816/cOO1F28S_bigger.png'}, content: '《绝代双娇》第一高手，当世江湖第一神剑。实力相当于2个绝顶高手。', url: 'https://twitter.com/forum_hc/status/568806047051165696'},
            {user: {id: '大沙漠', name: '4、李商', avatar: 'https://pbs.twimg.com/profile_images/3133057797/81ea4e63c7078eec0a7c7d6ae57a3ce1_bigger.jpeg'}, content: '人称“石观音”，《大沙漠》第一高手，50招胜楚留香。实力相当于2个绝顶高手。', url: 'https://twitter.com/loicknuchel/status/568449174926176256'},
            {user: {id: '武林外史', name: '5、柴玉关', avatar: 'https://pbs.twimg.com/profile_images/378800000568876933/2da22327d055cbf8e0502c3f22888fef_bigger.jpeg'}, content: '人称“快活王”，《武林外史》第一高手。实力相当于2个绝顶高手。', url: 'https://twitter.com/raymondcamden/status/568424487693082625'},
            {user: {id: '幽灵山庄', name: '6、木道人', avatar: 'https://pbs.twimg.com/profile_images/2653730816/5da4d8fb72352c715bbaffe07e56270e_bigger.jpeg'}, content: '江湖人称“老刀把子”，武当第一名宿，《幽灵山庄》第一高手。实力相当于2个绝高手。', url: 'https://twitter.com/nraboy/status/568438200198258689'},
            {user: {id: '绝代双娇', name: '7、邀月宫主', avatar: 'https://pbs.twimg.com/profile_images/378800000664886768/5aa49c1cded0317a887cae28f5d80cd7_bigger.jpeg'}, content: '人称“绣玉谷”，移花宫的大宫主。实力相当于2个绝顶高手。', url: 'https://twitter.com/devgirlFL/status/563673910424928256'},
            {user: {id: '陆小凤传奇', name: '8、西门吹雪', avatar: 'https://pbs.twimg.com/profile_images/546942133496995840/k7JAxvgq_bigger.jpeg'}, content: '“万梅山庄”庄主，江湖第一剑神。实力相当于1.5个绝顶高手。', url: 'https://twitter.com/maxlynch/status/568097163131006976'},
            {user: {id: '剑客行', name: '9、展白', avatar: 'https://pbs.twimg.com/profile_images/519532575031713792/Fm4zj2Zm_bigger.jpeg'}, content: '《剑客行》第一高手，全书第一主人公。实力相当于1.5个绝顶高手。', url: 'https://twitter.com/asdvaughan/status/566103487281635328'},
            {user: {id: '名剑风流', name: '10、俞佩玉', avatar: 'https://pbs.twimg.com/profile_images/378800000664886768/5aa49c1cded0317a887cae28f5d80cd7_bigger.jpeg'}, content: '《名剑风流》第一高手，全书第一主人公。实力相当于1.5个绝顶高手。', url: 'https://twitter.com/devgirlFL/status/563673910424928256'},
        ];

        var service = {
            getTwitts: getTwitts,
            getNewTwitts: getNewTwitts,
            getMoreTwitts: getMoreTwitts
        };

        function getTwitts(){
            return $q.when(angular.copy(twitts));
        }

        function getNewTwitts(){
            var defer = $q.defer();
            $timeout(function(){
                var newTwitt = angular.copy(twitts[Math.floor(Math.random()*twitts.length)]);
                defer.resolve([newTwitt]);
            }, 500);
            return defer.promise;
        }

        function getMoreTwitts(){
            var defer = $q.defer();
            $timeout(function(){
                var newTwitts = [];
                for(var i=0; i<5; i++){
                    newTwitts.push(angular.copy(twitts[Math.floor(Math.random()*twitts.length)]));
                }
                defer.resolve(newTwitts);
            }, 500);
            return defer.promise;
        }

        return service;
    })

    .controller('AppCtrl', function($scope, TwittSrv){
        'use strict';
        TwittSrv.getTwitts().then(function(twitts){
            $scope.twitts =twitts ;
        });

        $scope.doRefresh = function(){
            TwittSrv.getNewTwitts().then(function(newTwitts){
                $scope.twitts = newTwitts.concat($scope.twitts);
            }).finally(function() {
                // Stop the ion-refresher from spinning
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        $scope.loadMore = function(){
            TwittSrv.getMoreTwitts().then(function(olderTwitts){
                $scope.twitts = $scope.twitts.concat(olderTwitts);
            }).finally(function() {
                // Stop the ion-infinite-scroll from spinning
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        };
    });
